package org.itboys.mobile.controller.site;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.JoinMemberExercise;
import org.itboys.mobile.service.mongo.site.JoinSiteMemberExerciseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/9/28
 * 描述：雄安加盟网_个人经历
 */
@Controller
@RequestMapping("/join/site/exercise")
public class JoinSiteExerciseController extends BaseController{
    @Autowired
    private JoinSiteMemberExerciseService exerciseService;

    /**
     * 新增
     * @param request
     * @param response
     * @param memberId
     * @param categoryId
     * @param categoryName
     * @param startTime
     * @param endTime
     * @param amount
     * @param type
     */
    @RequestMapping("/save")
    public void save(HttpServletRequest request,
                     HttpServletResponse response,
                     @RequestParam(value="memberId",required=true)Long memberId,
                     @RequestParam(value="categoryId",required=true)Long categoryId,
                     @RequestParam(value="categoryName",required=true)String categoryName,
                     @RequestParam(value="startTime",required=true)String startTime,
                     @RequestParam(value="endTime",required=true)String endTime,
                     @RequestParam(value="amount",required=true)Double amount,
                     @RequestParam(value="type",required=true)String type
    ){
        try {
            exerciseService.save(new JoinMemberExercise(memberId,categoryId,categoryName,startTime,endTime,amount,type));
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));

        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/join/site/exercise/save------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));;
        }

    }

    /**
     * 查询
     * @param request
     * @param response
     * @param memberId
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="memberId",required=true)Long memberId
    ) {
        try {
            Map<String,Object> param=new HashMap<String,Object>();
            param.put("isDeleted", SundayCommonConstants.NO);
            param.put("memberId",memberId);
            List<JoinMemberExercise> result=exerciseService.list(param);
            AjaxUtils.renderJson(response,result);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    /**
     *删除
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="id",required=true) Long id

    ) {
        try {
            exerciseService.delete(id);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {

            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/join/site/exercise/delete------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

}
